package so.sao.code.service.service.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import so.sao.code.dto.enums.ThirdPartyCodeFileStatus;
import so.sao.code.service.AppExecutors;
import so.sao.code.service.domain.ThirdPartyCodeFileInfoWrapper;
import so.sao.code.service.domain.UserBaseInfoWrapper;
import so.sao.code.service.domain.entity.CodeSupplierConfig;
import so.sao.code.service.domain.entity.ThirdPartyCodeFileRecord;
import so.sao.code.service.repository.CodeSupplierConfigDao;
import so.sao.code.service.repository.ThirdPartyCodeFileRecordDao;
import so.sao.code.service.service.ThirdPartyCodeErrorResolveService;
import so.sao.code.service.service.ThirdPartyCodeFileHandleService;

@Service
public class ThirdPartyCodeErrorResolveServiceImpl implements ThirdPartyCodeErrorResolveService {

	private static Logger logger = LoggerFactory.getLogger(ThirdPartyCodeErrorResolveServiceImpl.class);

	@Autowired
	private ThirdPartyCodeFileRecordDao thirdPartyCodeFileRecordDao;

	@Autowired
	private CodeSupplierConfigDao codeSupplierConfigDao;

	@Resource(name="thirdPartyCodeFilePackageHandle")
	ThirdPartyCodeFileHandleService thirdPartyCodeFileHandleService;

	public void resolveTask(Long recordId, boolean isForce) {
		ThirdPartyCodeFileRecord record = null;
		try {
			record = thirdPartyCodeFileRecordDao.findOne(recordId);
			if (record == null) {
				return;
			}
			
			if (record == null || record.getException() == null) {
				return;
			}
			// 是否强制。
			if (!isForce) {
				// 不强制
				if (record.getException() == null || record.getException()
						.intValue() == ThirdPartyCodeFileRecord.CodeException.NO_EXCEPTION.getValue()) {
					return;
				}
			}

			record.setException(ThirdPartyCodeFileRecord.CodeException.NO_EXCEPTION.getValue());
			record = thirdPartyCodeFileRecordDao.save(record);
		} catch (Exception e1) {
			logger.error(e1.getMessage(), e1);
			// 如果已经被消费。
			throw e1;
		}

		try {
			CodeSupplierConfig config = codeSupplierConfigDao.findByCompanyId(record.getCompanyId());
			logger.info(String.valueOf(record.getVersion()));
			UserBaseInfoWrapper user = new UserBaseInfoWrapper();
			user.setConstantConfig(config);
			user.setEseId(record.getEseId());
			user.setUserId(record.getCsId());
			ThirdPartyCodeFileInfoWrapper wrapper = new ThirdPartyCodeFileInfoWrapper();
			wrapper.setRecord(record);

			// 分类进行job调用。
			// 1校验失败
			if (record.getStatus().getValue() < ThirdPartyCodeFileStatus.MAPPING.getValue()
					&& record.getStatus().getValue() > ThirdPartyCodeFileStatus.INIT_FILE.getValue()
					&& record.getStatus().getValue() != ThirdPartyCodeFileStatus.VALIDATE_OK.getValue()) {
				// 解析
				// PackageFileProcessTask task = new
				// PackageFileProcessTask(user, wrapper);
				AppExecutors.getTaskPoolExecutor().execute(() -> {
					try {
						thirdPartyCodeFileHandleService.doProcess(user, wrapper);
					} catch (Exception e) {
						logger.error(e.getMessage(), e);
					}
				});
				// 2 mapping失败
			} else if (record.getStatus().getValue() < ThirdPartyCodeFileStatus.SEND.getValue()
					&& record.getStatus().getValue() > ThirdPartyCodeFileStatus.MAPPING.getValue()
					&& record.getStatus().getValue() != ThirdPartyCodeFileStatus.MAPPING_OK.getValue()) {

				AppExecutors.getTaskPoolExecutor().execute(() -> {
					try {
						thirdPartyCodeFileHandleService.mapping(user, wrapper);
						thirdPartyCodeFileHandleService.sendFFW(user, wrapper);
						thirdPartyCodeFileHandleService.pushSignmatrix(user, wrapper);
					} catch (Exception e) {
						logger.error(e.getMessage(), e);
					}
				});
				// 3发送ffw失败
			} else if (record.getStatus().getValue() == ThirdPartyCodeFileStatus.SEND_NG.getValue()) {
				thirdPartyCodeFileHandleService.sendFFW(user, wrapper);
				thirdPartyCodeFileHandleService.pushSignmatrix(user, wrapper);

				// 4,ffw处理失败
			} else if (record.getStatus().getValue() == ThirdPartyCodeFileStatus.FFW_FAILD.getValue()) {
				thirdPartyCodeFileHandleService.sendFFW(user, wrapper);
				// 5,发送供应链失败。
			} else if (record.getStatus().getValue() == ThirdPartyCodeFileStatus.SEND_SM_NG.getValue()) {
				thirdPartyCodeFileHandleService.pushSignmatrix(user, wrapper);
			} else {
				logger.info("该记录不符合处理要求。recordId: " + record.getId());
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw e;
		} finally {

		}

	}

}
